<?php 
	class Books extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	function get_books($limit = 0, $offset = 0, $conditions = array()){
    		$where = "status = 1";
    		if(!empty($conditions['subject_id'])){
    			$where .= " AND subject_id = '{$conditions['subject_id']}' ";
    		}
    		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}b_book ";
			$sql .= "WHERE $where ORDER BY id DESC ";
			if($limit > 0 || $offset > 0){
			   $sql .= "LIMIT $limit, $offset";
			}
			$result = $this->db->query($sql)->result_array();
			$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
			return array("count" => $row_num['row_num'], "result" => $result);
    	}

    	//章节练习题目列表
    	function get_book_chapter_list($limit = 0, $offset = 0, $conditions = array()){
    		$where = "c.status = 1 ";
    		if(!empty($conditions['book_id'])){
    			$where .= " AND c.book_id = '{$conditions['book_id']}' ";
    		}
    		$sql = "SELECT SQL_CALC_FOUND_ROWS c.*, count(tm.id) AS total FROM {$this->db->dbprefix}b_book_chapter AS c ";
    		$sql .= "LEFT JOIN {$this->db->dbprefix}tk_timu_main AS tm ON(tm.book_chapter_id=c.id AND tm.status = 1) ";
			$sql .= "WHERE $where GROUP BY c.id ORDER BY id ASC ";
			if($limit > 0 || $offset > 0){
			   $sql .= "LIMIT $limit, $offset";
			}
			$result = $this->db->query($sql)->result_array();
			$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
			return array("count" => $row_num['row_num'], "result" => $result);
    	}
    }